/**
 * 
 */
package com.byzk.p2p.admin.user.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byzk.p2p.admin.base.util.BaseUtil;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.city.dto.City;
import com.byzk.p2p.admin.city.service.CityService;
import com.byzk.p2p.admin.fuiou.config.Config;
import com.byzk.p2p.admin.log.dao.ThirdMsgRecordDao;
import com.byzk.p2p.admin.log.dto.ThirdMsgRecord;
import com.byzk.p2p.admin.money.util.FundRecordType;
import com.byzk.p2p.admin.user.dto.User;

/***
 * <p>
 * Title:NoticeCallBack
 * </p>
 * <p>
 * Description: 通知回调服务类
 * </p>
 * <p>
 * Company:xbd
 * </p>
 * 
 * @author:Administrator
 * @date:2016年10月17日上午8:31:36
 */
@Service
public class NoticeCallBackService {

	@Autowired
	private UserService userService;
	@Autowired
	private ThirdMsgRecordDao recordDao;
	
	@Autowired 
	private CityService cityService;
	
	

	private static Logger logger = Logger.getLogger(NoticeCallBackService.class);

	/**
	 * <p>
	 * Description:充值记录加入到资金明细中
	 * </p>
	 * <p>
	 * Company:xbd
	 * </p>
	 * 
	 * @author:lilei
	 * @throws ParseException
	 * @date:2016年10月17日上午11:52:21
	 */
	public void recharge(Map<String, String> req) throws ParseException {
		if (req != null) {

			User user = userService.getUserByPhone(req.get("mobile_no"));
			if (user == null) {
				logger.info("=======不存在该用户的信息=====" + (req.get("mobile_no") + ""));
			}
			ThirdMsgRecord record = new ThirdMsgRecord();

			record.setCreateTime(DateUtils.convert(req.get("mchnt_txn_dt"), DateUtils.DATE_FORMAT));
			record.setContent("用户【" + user.getUsername() + "】,金账户充值，记录流水号【" + req.get("mchnt_txn_ssn") + "】,充值金额【"
					+ Double.parseDouble(req.get("amt")) / 100 + "】");
			record.setType(FundRecordType.Type.RECHARGE.index + "");
			record.setPhone(req.get("mobile_no"));
			logger.info("记录:手机号：" + record.getPhone() + "类型：" + record.getType() + "内容：" + record.getContent());
		    List<ThirdMsgRecord> list = recordDao.recordListByTerm(new HashMap<String,Object>());
		    if(list!=null && list.size()>0){
		    	for(ThirdMsgRecord item :list){
		    		if(StringUtils.isNotBlank(req.get("mchnt_txn_ssn")) && (item.getContent()).contains(req.get("mchnt_txn_ssn")) ){
		    			return;
		    		}
		    	}
		    }
			recordDao.insert(record);
		}
	}

	/**
	 * <p>
	 * Description:提现记录加入到资金明细中
	 * </p>
	 * <p>
	 * Company:xbd
	 * </p>
	 * 
	 * @author:lilei
	 * @throws ParseException
	 * @date:2016年10月17日下午2:03:57
	 */
	public void withDraw(Map<String, String> req) throws ParseException {
		if (req != null) {
			User user = userService.getUserByPhone(req.get("mobile_no"));
			if (user == null) {
				logger.info("=======不存在该用户的信息=====" + (req.get("mobile_no") + ""));
			}
			ThirdMsgRecord record = new ThirdMsgRecord();
			record.setCreateTime(DateUtils.convert(req.get("mchnt_txn_dt"), DateUtils.DATE_FORMAT));
			record.setContent("用户【" + user.getUsername() + "】,金账户提现，记录流水号【" + req.get("mchnt_txn_ssn") + "】,提现金额【"
					+ Double.parseDouble(req.get("amt")) / 100 + "】");
			record.setType(FundRecordType.Type.WITHDRAW.index + "");
			record.setPhone(req.get("mobile_no"));
			logger.info("记录:手机号：" + record.getPhone() + "类型：" + record.getType() + "内容：" + record.getContent());
		    List<ThirdMsgRecord> list = recordDao.recordListByTerm(new HashMap<String,Object>());
		    if(list!=null && list.size()>0){
		    	for(ThirdMsgRecord item :list){
		    		if(StringUtils.isNotBlank(req.get("mchnt_txn_ssn")) && (item.getContent()).contains(req.get("mchnt_txn_ssn")) ){
		    			return;
		    		}
		    	}
		    }
			recordDao.insert(record); 
		}
	}

	/**
	 * 
	 * <p>
	 * Description:变更银行卡服务
	 * </p>
	 * <p>
	 * Company:xbd
	 * </p>
	 * 
	 * @author:lilei
	 * @throws ParseException
	 * @date:2016年10月17日下午3:58:55
	 */
	public void modifyBank(Map<String, String> req) throws ParseException, Exception {

		if (req != null) {
			User user = userService.getUserByPhone(req.get("mobile_no"));
			if (user == null) {
				logger.info("=======不存在该用户的信息===== 手机号：" + (req.get("mobile_no") + ""));
			}
			City city = new City();//
			ThirdMsgRecord record = new ThirdMsgRecord();
			if (req.get("city_id") != null && !StringUtils.isBlank(req.get("city_id"))) {
				city.setCode(req.get("city_id"));
				city = cityService.listAll(city).get(0);
			}
			record.setCreateTime(DateUtils.convert(req.get("mchnt_txn_dt"), DateUtils.DATE_FORMAT));
			record.setContent("用户【" + req.get("cust_nm") + "】,【身份证号：" + req.get("certif_id") + "】,商户系统标志:【"
					+ req.get("user_id_from") + "】开户城市代码:【" + req.get("city_id") + "】,开户城市:【" + city.getName()
					+ "】,开户行行别：【" + req.get("parent_bank_id") + "开户行支行名称:" + req.get("bank_nm") + ",银行卡号:"
					+ req.get("capAcntNo") + "】，记录流水号【" + req.get("mchnt_txn_ssn") + "】");
			record.setType(FundRecordType.Type.OTHER.index + "");
			record.setPhone(req.get("mobile_no"));
			logger.info("记录:手机号：" + record.getPhone() + "类型：" + record.getType() + "内容：" + record.getContent());
		    List<ThirdMsgRecord> list = recordDao.recordListByTerm(new HashMap<String,Object>());
		    if(list!=null && list.size()>0){
		    	for(ThirdMsgRecord item :list){
		    		if(StringUtils.isNotBlank(req.get("mchnt_txn_ssn")) && (item.getContent()).contains(req.get("mchnt_txn_ssn")) ){
		    			return;
		    		}
		    	}
		    }
			recordDao.insert(record);
		}
	}
	/**
	 * <p>
	 * Description: 响应请求返回
	 * </p>
	 * <p>
	 * Company:xbd
	 * </p>
	 * 
	 * @author:lilei
	 * @throws IOException
	 * @date:2016年10月17日下午1:43:37
	 */
	public void responseSucc(HttpServletResponse response, Map<String, String> reqParam) throws IOException {

		String strXML = "<?xml version='1.0' encoding='UTF-8'?>";
		strXML += "<ap>";
		strXML += "<plain>";
		strXML += "<resp_code>0000</resp_code>";
		strXML += "<mchnt_cd>" + Config.mchnt_cd + "</mchnt_cd>";
		strXML += "<mchnt_txn_ssn>" + DateUtils.getFullFormat() + (new Random().nextInt(8999) + 1000)
				+ "</mchnt_txn_ssn>";
		strXML += "/plain>";
		strXML += "<signature>" + reqParam.get("signature") + "</signature>";
		strXML += "</ap>";
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml;charset=UTF-8");
		out.println(strXML);
	}
}
